﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Xml.Serialization;
using System.IO;
using System.Web;
using System.Xml;
using System.Data.SqlClient;
using VPLink2013.Framework;

namespace VPLink2013.Webpart
{
    /// <summary>
    /// 
    /// </summary>
    public class MenuInterne : WebpartVPLink
    {
        #region get HTML
        /// <summary>
        /// Returns the HTML code rendered by this web part
        /// </summary>
        /// <returns>HTL code as string</returns>
        protected override string GetHtml()
        {
            ContextTrace.PrefixFileName = "webpart MenuInterne";
            string strHtml = ContextTrace.PrefixFileName;

            try
            {
                strFileCacheUtilisateur = pathCache + @"\MenuGeneralUtilisateur\MenuGeneral_"
                    + StringAfter(this.Context.User.Identity.Name) + ".xml";
                FileSerialization.CheckFileCacheExists(strFileCacheUtilisateur, 60 * 12);

                SPSecurity.RunWithElevatedPrivileges
                (delegate()
                {
                    SetUtilisateurCrt();
                });

                string strHtmlFileCache = pathCache + @"\MenuGeneral\MenuGeneral_"
                    + "Interne" + UtilisateurCrt.IsInterne
                    + "_Manager" + UtilisateurCrt.IsManager
                    + "_DRH" + UtilisateurCrt.IsDRH
                    + ".htm";

                strHtml = ProcessMenuGeneral();
                TraceFile(strHtmlFileCache, strHtml);

            }
            catch (System.Exception exc)
            {
                ContextTrace.AddTrace("Erreur GetHtml : " + exc.Message + "/" + exc.StackTrace);
            }
            return strHtml;
        }
        #endregion get HTML


        private string ProcessMenuGeneral()
        {
            ContextTrace.AddTrace("ProcessMenu.UtilisateurCrt.IsInterne : " + UtilisateurCrt.IsInterne);
            ContextTrace.AddTrace("ProcessMenu.UtilisateurCrt.IsManager : " + UtilisateurCrt.IsManager);

            string strPageCrt = this.Context.Request.Url.AbsolutePath;
            string strItemID = ParamFrm("ItemID");
            if (!string.IsNullOrEmpty(strItemID))
            {
                strPageCrt += "?ItemID=" + strItemID;
            }
            string strCategorie = ParamFrm("Categorie");
            if (strCategorie != "")
            {
                strPageCrt += "?Categorie=" + strCategorie;
            }
            strPageCrt = strPageCrt.ToLower();
            ContextTrace.AddTrace("strPageCrt : " + strPageCrt);


            string strXml = string.Empty;

            string fileName = pathVP + @"\XSL\MenuGeneral\MenuGeneral.xml";
            ContextTrace.AddTrace("doc.Load : " + fileName);
            XmlDocument doc = new XmlDocument();
            doc.Load(fileName);

            foreach (XmlNode nod1 in doc.ChildNodes)
            {
                ContextTrace.AddTrace(" nod1." + nod1.Name);
                foreach (XmlNode nod2 in nod1.ChildNodes)
                {
                    string strName2 = GetNodeAttribute(nod2, "name");
                    string strLink2 = GetNodeAttribute(nod2, "link");
                    string strInternalOnly2 = GetNodeAttribute(nod2, "internonly");
                    string strManagerOnly2 = GetNodeAttribute(nod2, "manageronly");
                    string strDrhOnly2 = GetNodeAttribute(nod2, "drhonly"); //Marie Pierre Bonnazi seulement

                    ContextTrace.AddTrace("  nod2." + nod2.Name + " => " + strName2
                        + " / strLink2 : " + strLink2
                        + " / strInternalOnly2 : " + strInternalOnly2
                        + " / strManagerOnly2 : " + strManagerOnly2
                        + " / strDrhOnly2 : " + strDrhOnly2);

                    XmlAttribute attrNamePiwik2 = doc.CreateAttribute("namepiwik");
                    attrNamePiwik2.Value = strName2.Replace("/", "-").Replace("'", "\\'").Replace("\"", "\\'");
                    nod2.Attributes.Append(attrNamePiwik2);
                    ContextTrace.AddTrace("nod2.attrNamePiwik2 : " + attrNamePiwik2.Value);

                    if ((UtilisateurCrt.IsManager != "1" && strManagerOnly2 == "true")
                        || (UtilisateurCrt.IsInterne != "1" && strInternalOnly2 == "true")
                        || (UtilisateurCrt.IsDRH != "1" && strDrhOnly2 == "true")
                        )
                    {
                        ContextTrace.AddTrace("nod1.RemoveChild");
                        XmlAttribute attrDeleted2 = doc.CreateAttribute("suppression");
                        attrDeleted2.Value = "true";
                        nod2.Attributes.Append(attrDeleted2);
                    }
                    else
                    {
                        if (strLink2.Equals(strPageCrt, StringComparison.CurrentCultureIgnoreCase))
                        {
                            XmlAttribute attrSelected2 = doc.CreateAttribute("class");
                            attrSelected2.Value = "currentinterne1";
                            nod2.Attributes.Append(attrSelected2);
                            ContextTrace.AddTrace("nod2.current");
                        }

                        foreach (XmlNode nod3 in nod2.ChildNodes)
                        {
                            string strName3 = GetNodeAttribute(nod3, "name");
                            string strLink3 = GetNodeAttribute(nod3, "link");
                            string strInternalOnly3 = GetNodeAttribute(nod3, "internonly");
                            string strManagerOnly3 = GetNodeAttribute(nod3, "manageronly");
                            string strDrhOnly3 = GetNodeAttribute(nod3, "drhonly"); //Marie Pierre Bonnazi seulement
                            ContextTrace.AddTrace("   nod3." + nod3.Name + " => " + strName3
                                + " / strLink3 : " + strLink3
                                + " / strInternalOnly3 : " + strInternalOnly3
                                + " / strManagerOnly3 : " + strManagerOnly3
                                + " / strDrhOnly3 : " + strDrhOnly3);

                            XmlAttribute attrNamePiwik3 = doc.CreateAttribute("namepiwik");
                            attrNamePiwik3.Value = strName3.Replace("/", "-").Replace("'", "\\'").Replace("\"", "\\'");
                            nod3.Attributes.Append(attrNamePiwik3);
                            ContextTrace.AddTrace("nod3.attrNamePiwik3 : " + attrNamePiwik3.Value);

                            if ((UtilisateurCrt.IsManager != "1" && strManagerOnly3 == "true")
                                || (UtilisateurCrt.IsInterne != "1" && strInternalOnly3 == "true")
                                || (UtilisateurCrt.IsDRH != "1" && strDrhOnly3 == "true")
                                )
                            {
                                XmlAttribute attrDeleted3 = doc.CreateAttribute("suppression");
                                attrDeleted3.Value = "true";
                                nod3.Attributes.Append(attrDeleted3);
                                ContextTrace.AddTrace("nod2.RemoveChild");
                            }
                            else
                            {
                                if (strLink3.Equals(strPageCrt, StringComparison.CurrentCultureIgnoreCase))
                                {
                                    XmlAttribute attrSelected2 = doc.CreateAttribute("class");
                                    attrSelected2.Value = "currentinterne1";
                                    nod2.Attributes.Append(attrSelected2);
                                    ContextTrace.AddTrace("nod2.current");

                                    XmlAttribute attrSelected3 = doc.CreateAttribute("class");
                                    attrSelected3.Value = "active";
                                    nod3.Attributes.Append(attrSelected3);
                                    ContextTrace.AddTrace("nod3.current");
                                }

                                foreach (XmlNode nod4 in nod3.ChildNodes)
                                {
                                    string strName4 = GetNodeAttribute(nod4, "name");
                                    string strLink4 = GetNodeAttribute(nod4, "link");
                                    string strInternalOnly4 = GetNodeAttribute(nod4, "internonly");
                                    string strManagerOnly4 = GetNodeAttribute(nod4, "manageronly");
                                    string strTools4 = GetNodeAttribute(nod4, "tools");
                                    string strDrhOnly4 = GetNodeAttribute(nod4, "drhonly"); //Marie Pierre Bonnazi seulement

                                    XmlAttribute attrNamePiwik4 = doc.CreateAttribute("namepiwik");
                                    attrNamePiwik4.Value = strName4.Replace("/", "-").Replace("'", "\\'").Replace("\"", "\\'");
                                    nod4.Attributes.Append(attrNamePiwik4);
                                    ContextTrace.AddTrace("nod4.attrNamePiwik4 : " + attrNamePiwik4.Value);

                                    if ((UtilisateurCrt.IsManager != "1" && strManagerOnly3 == "true")
                                        || (UtilisateurCrt.IsInterne != "1" && strInternalOnly3 == "true")
                                        || (UtilisateurCrt.IsDRH != "1" && strDrhOnly3 == "true")
                                        )
                                    {
                                        XmlAttribute attrDeleted4 = doc.CreateAttribute("suppression");
                                        attrDeleted4.Value = "true";
                                        nod4.Attributes.Append(attrDeleted4);
                                        ContextTrace.AddTrace("nod3.RemoveChild");
                                    }
                                    else
                                    {
                                        if (strLink4.Equals(strPageCrt, StringComparison.CurrentCultureIgnoreCase))
                                        {
                                            XmlAttribute attrSelected2 = doc.CreateAttribute("class");
                                            attrSelected2.Value = "currentinterne1";
                                            nod2.Attributes.Append(attrSelected2);
                                            ContextTrace.AddTrace("nod2.current");

                                            XmlAttribute attrSelected3 = doc.CreateAttribute("class");
                                            attrSelected3.Value = "active";
                                            nod3.Attributes.Append(attrSelected3);
                                            ContextTrace.AddTrace("nod3.current");

                                            XmlAttribute attrSelected4 = doc.CreateAttribute("class");
                                            attrSelected4.Value = "active";
                                            nod4.Attributes.Append(attrSelected4);
                                            ContextTrace.AddTrace("nod4.current");
                                        }
                                        if (strTools4 == "true")
                                        {
                                            XmlAttribute attrLastLink4 = doc.CreateAttribute("lastlink");
                                            attrLastLink4.Value = "last-link";
                                            nod4.Attributes.Append(attrLastLink4);
                                            ContextTrace.AddTrace("nod4.last-link");
                                        }
                                        ContextTrace.AddTrace("    nod4." + nod4.Name + " => " + strName4
                                            + " / strLink4 : " + strLink4
                                            + " / strInternalOnly4 : " + strInternalOnly4
                                            + " / strManagerOnly4 : " + strManagerOnly4
                                            + " / strDrhOnly4 : " + strDrhOnly4);
                                    }
                                }
                            }
                        }
                    }
                }
                if (nod1.Name == "menu")
                {
                    strXml = nod1.OuterXml;
                    ContextTrace.AddTrace("strXml : " + strXml);
                }
            }

            string strXslPath = pathVP + @"\XSL\MenuInterne\MenuInterne.xsl";
            ContextTrace.AddTrace("GetHtml.strXslPath = " + strXslPath);
            return VPLink2013XsltTransformHelper.GetTransform(strXml, strXslPath, StringAfter(UtilisateurCrt.Login), pathTemp); 
        }

        private string GetNodeAttribute(XmlNode nod, string name)
        {
            string strValue = string.Empty;
            try
            {
                strValue = nod.Attributes[name].Value;
                if (string.IsNullOrEmpty(strValue))
                {
                    strValue = string.Empty;
                }
            }
            catch (System.Exception) { }
            return strValue;
        }
    }
}
